<template>
  <div>
    <form-head @onSubmit="onSubmit" :is_change="is_change"/>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" class="layout-all form-width">
      <el-form-item label="团购名:" prop="title">
        <el-input v-model="form.title" placeholder="请输入团购名" />
      </el-form-item>
      <el-form-item label="指定商品:" prop="goods_id">
        <el-select v-model="form.goods_id" filterable placeholder="请选择指定商品">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.title"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="团购类型:" prop="type" >
      <el-radio-group v-model="form.type">
        <el-radio v-for="(item,index) in type_arr" :key="index" :label="index">{{item}}</el-radio>
      </el-radio-group>
      <el-tag v-if="form.type==1" >超级团购:随机一人成功购买商品,其它人自动退款并得到分佣金额的{{form.commission_prop}}%</el-tag>
      </el-form-item>
      <el-form-item v-if="form.type==1" label="分佣比例:" prop="commission_prop">
        <el-input v-model="form.commission_prop" placeholder="请输入分佣比例" />
      </el-form-item>
      <el-form-item label="封面:" prop="thumb">
        <upload-pic :thumb="form.thumb" @update:thumb="form.thumb=$event" width="100"/>
        <div style="font-size:12px;">建议尺寸：110x110</div>
      </el-form-item>
      <el-form-item label="开始时间:" prop="start_time">
        <el-date-picker
          v-model="form.start_time"
          type="datetime"
          placeholder="选择开始时间"
          default-time="12:00:00"
          @change="changeStartTime()"
        />
      </el-form-item>
      <el-form-item label="结束时间:" prop="end_time">
        <el-date-picker
          v-model="form.end_time"
          type="datetime"
          placeholder="选择结束时间"
          default-time="12:00:00"
          @change="changeEndTime()"
        />
      </el-form-item>
      <el-form-item label="拼团时间(小时):" prop="group_time">
        <el-input v-model="form.group_time" placeholder="请输入拼团时间" />
      </el-form-item>
      <el-form-item label="同时最大拼团个数:" prop="group_time">
        <el-input v-model="form.max_group_ing" placeholder="请输入同时最大拼团个数(默认为不限制)" />
      </el-form-item>
      <el-form-item label="价格:" prop="real_price">
        <el-input v-model="form.real_price" placeholder="请输入价格" />
      </el-form-item>

      <el-form-item label="团购人数:" prop="people_count">
        <el-input-number v-model="form.people_count" placeholder="请输入团购人数" />
      </el-form-item>
      <el-form-item label="成团数:" prop="group_count">
        <el-input-number v-model="form.group_count" placeholder="请输入成团数" />
      </el-form-item>
      <el-form-item label="最大成团数:" prop="max_group_count">
        <el-input-number v-model="form.max_group_count" placeholder="请输入最大成团数" />
      </el-form-item>
      <el-form-item label="是否启用:" prop="status">
        <el-switch
          v-model="form.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name:"group_buy_form",
  data() {
    return {
      id: 0,
      is_change: undefined,
      super_group_commission: 0,
      options: [],
      type_arr: [],
      url: '',
      thumb: '',
      form: {
        status:true,
        type:0
      },
      rules: {
        title: [
          { required: true, message: '请输入商品标题', trigger: 'blur' }
        ],
        goods_id: [
          { required: true, message: '请选择指定商品', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ],
        end_time: [
          { required: true, message: '请选择结束时间', trigger: 'blur' }
        ],
        group_time: [
          { required: true, message: '请输入拼团时间', trigger: 'blur' }
        ],
        real_price: [
          { required: true, message: '请输入价格', trigger: 'blur' }
        ],
        people_count: [
          { required: true, message: '请输入团购人数', trigger: 'blur' }
        ],
        group_count: [
          { required: true, message: '请输入成团数', trigger: 'blur' }
        ],
        max_group_count: [
          { required: true, message: '请输入最大成团数', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.id = (this.$route.params && this.$route.params.id)||0
    this.$setTagsViewTitle(this.id)
    this.init_into()
  },
  methods: {
    changeStartTime() {
      const date = new Date(this.form.start_time)
      this.form.start_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },
    changeEndTime() {
      const date = new Date(this.form.end_time)
      this.form.end_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },

    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          form.status === true ? form.status = 1 : form.status = 0
          if(form.max_group_ing=='')form.max_group_ing=0
          this.get('/shop/GoodsGroup/save', form, 'POST').then(e => {
            this.$back()
          })
          
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/shop/GoodsGroup/info?id=' + this.id, 'POST').then(e => {
        this.options = e.goods
        this.type_arr = e.type_arr
        this.super_group_commission = e.super_group_commission
        const tpl = e.info.tpl
        if(this.id>0){
          this.form =tpl
          this.form.status =this.form.status==1
          if(this.form.goods_id==0)this.form.goods_id=''
          if(this.form.max_group_ing==0)this.form.max_group_ing=''

        }
        if(this.form.commission_prop==undefined||this.form.commission_prop==0){
          this.form.commission_prop=this.super_group_commission
        }
        setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
        if(this.is_change!=undefined)
        this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
